﻿using System;
using System.Collections.Generic;
using System.Text;

namespace OestsCommon.TcpNet
{

    //接收到的数据转换为字节数组
    public class Message
    {
        //存储接收到的数据
        private byte[] data = new byte[1024 * 1024*2];
        private int startIndex = 0;//存储位置的开始索引，也表示已存储数据的长度

        public byte[] Data
        {
            get { return data; }
        }
        public int StartIndex
        {
            get { return startIndex; }
        }

        //闲置空间长度
        public int RemainSize
        {
            get { return data.Length - startIndex; }
        }
        public void ParseFromClientMessage(int newDataLength, Action<Request> processDataCallback)
        {
            startIndex += newDataLength;
            while (true)
            {
                if (startIndex <= 4) return;
                //需要处理数据的长度
                int count = BitConverter.ToInt32(data, 0);
                if ((startIndex - 4) >= count)
                {
                    Request request = new Request()
                    {
                        RequestCode = (PacketKey)BitConverter.ToInt32(data, 4),
                        RequestSubCode = (SubCode)BitConverter.ToInt32(data, 8)
                    };
                    string dataStr = Encoding.UTF8.GetString(data, 12, count - 8);
                    request.DataObject = dataStr;
                    processDataCallback(request);
                    Array.Copy(data, count + 4, data, 0, startIndex - 4 - count);
                    startIndex -= (count + 4);
                }
                else
                {
                    break;
                }
            }
        }


        /// <summary>
        /// 解析从服务器来的数据
        /// </summary>
        public void ParseFromServerMessage(int newDataLength, Action<Response> processDataCallback)
        {
            startIndex += newDataLength;
            while (true)
            {
                if (startIndex <= 4) return;
                //需要处理数据的长度
                int count = BitConverter.ToInt32(data, 0);
                if ((startIndex - 4) >= count)
                {
                    Response p = new Response()
                    {
                        ResponseCode = (PacketKey)BitConverter.ToInt32(data, 4),
                        ResponseSubCode = (SubCode)BitConverter.ToInt32(data, 8),
                        ReturnCode = (ReturnCode)BitConverter.ToInt32(data, 12)
                    };
                    int returnMessageLenth = BitConverter.ToInt32(data, 16);
                    p.RetrunMessage = Encoding.UTF8.GetString(data, 20, returnMessageLenth);
                    string dataStr = Encoding.UTF8.GetString(data, 20 + returnMessageLenth, count - 16 - returnMessageLenth);
                    p.DataObject = dataStr;
                    processDataCallback?.Invoke(p);
                    Array.Copy(data, count + 4, data, 0, startIndex - 4 - count);
                    startIndex -= (count + 4);
                }
                else
                {
                    break;
                }
            }
        }

        public static byte[] ClientPackData(Request packet)
        {
            byte[] packetBytes = BitConverter.GetBytes((int)packet.RequestCode);
            byte[] subCodeBytes = BitConverter.GetBytes((int)packet.RequestSubCode);

            byte[] dataBytes = Encoding.UTF8.GetBytes(packet.DataStr);

            int dataAmount = 8 + dataBytes.Length;

            byte[] dataAmountBytes = BitConverter.GetBytes(dataAmount);

            byte[] newBytes = new byte[dataAmount + 4];

            int index = 0;
            dataAmountBytes.CopyTo(newBytes, index);
            index += dataAmountBytes.Length;

            packetBytes.CopyTo(newBytes, index);
            index += packetBytes.Length;

            subCodeBytes.CopyTo(newBytes, index);
            index += subCodeBytes.Length;

            dataBytes.CopyTo(newBytes, index);

            return newBytes;
        }


        public static byte[] ServerPackData(Response packet)
        {
            byte[] packetBytes = BitConverter.GetBytes((int)packet.ResponseCode);
            byte[] subCodeBytes = BitConverter.GetBytes((int)packet.ResponseSubCode);
            byte[] returnCodeBytes = BitConverter.GetBytes((int)packet.ReturnCode);

            byte[] returnMessageBytes = Encoding.UTF8.GetBytes(packet.RetrunMessage);

            byte[] returnMessageLengthBytes = BitConverter.GetBytes(returnMessageBytes.Length);

            byte[] dataBytes = Encoding.UTF8.GetBytes(packet.DataStr);

            int dataAmount = packetBytes.Length + subCodeBytes.Length + returnCodeBytes.Length + returnMessageLengthBytes.Length + returnMessageBytes.Length + dataBytes.Length;

            byte[] dataAmountBytes = BitConverter.GetBytes(dataAmount);

            byte[] newBytes = new byte[dataAmount + 4];

            int index = 0;
            dataAmountBytes.CopyTo(newBytes, index);
            index += dataAmountBytes.Length;

            packetBytes.CopyTo(newBytes, index);
            index += packetBytes.Length;

            subCodeBytes.CopyTo(newBytes, index);
            index += subCodeBytes.Length;

            returnCodeBytes.CopyTo(newBytes, index);
            index += returnCodeBytes.Length;

            returnMessageLengthBytes.CopyTo(newBytes, index);
            index += returnMessageLengthBytes.Length;

            returnMessageBytes.CopyTo(newBytes, index);
            index += returnMessageBytes.Length;

            dataBytes.CopyTo(newBytes, index);

            return newBytes;
        }
    }
}
